<template>
  <div class="box">
    <!-- <div class="header-name">
      <div>
        <el-avatar
          src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"
        ></el-avatar>
        <p class="name">经纪人大数据服务系统</p>
      </div>
    </div> -->
    <!--TODO 错误 --->
    <div class="tag-group">
      <el-tag @click="workbench" class="work workActive">{{
        workLabel.label
      }}</el-tag>
      <el-tag @click="webBig" class="work workActive" v-if="showBig"
        >监控系统</el-tag
      >
      <el-tag
        v-for="(item, index) in data"
        :key="index"
        effect="plain"
        closable
        class="scrollMenu active"
        @close="updateTag(item, index)"
        @click.native="clickTag(item)"
      >
        {{ item.label }}
      </el-tag>
    </div>
    <div class="header-right">
      <img class="headImg" :src="httpImg" @click="imgFun" />
      <p class="userName">{{ userName }}</p>
      <p @click="dialogVisible = true" class="el-icon-edit-outline p1">
        修改密码
      </p>
      <p @click="dialogTVisible = true" class="el-icon-edit-outline p1">
        修改授权密码
      </p>
      <p @click="signOut" class="el-icon-alarm-clock p1">退出</p>
    </div>
    <el-dialog
      title="修改密码"
      :visible.sync="dialogVisible"
      width="30%"
      :show-close="false"
      :close-on-click-modal="false"
      :append-to-body="true"
    >
      <el-form
        label-width="80px"
        :model="formLabelAlign"
        ref="formLabelAlign"
        :rules="rules"
      >
        <el-form-item label="旧密码" prop="oldPassword">
          <el-input
            v-model="formLabelAlign.oldPassword"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="newPassword">
          <el-input
            v-model="formLabelAlign.newPassword"
            type="password"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancle(noauthorize)">取 消</el-button>
        <el-button type="primary" @click="updateFun">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="修改授权密码"
      :visible.sync="dialogTVisible"
      width="30%"
      :show-close="false"
      :close-on-click-modal="false"
      :append-to-body="true"
    >
      <el-form
        label-width="80px"
        :model="formLabelAlignT"
        ref="formLabelAlignT"
        :rules="rules"
      >
        <el-form-item label="旧密码" prop="oldPassword">
          <el-input
            v-model="formLabelAlignT.oldPassword"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="newPassword">
          <el-input
            v-model="formLabelAlignT.newPassword"
            type="password"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancle(authorize)">取 消</el-button>
        <el-button type="primary" @click="updateAuthFun">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="修改头像"
      :visible.sync="imgDis"
      width="30%"
      :append-to-body="true"
      :before-close="handleClose"
    >
      <UploadAvatar @messageData="getData" ref="UploadAvatar"></UploadAvatar>
    </el-dialog>
  </div>
</template>
<script>
import {
  apiLogout,
  apiUpdatePassword,
  apiUpdatePasswordT,
  menuList,
  getIcon
  // getBigWbe
} from "../assets/js/api";
import imgSrc from "@/assets/img/userimg.png";
import $ from "jquery";
import UploadAvatar from "./UploadAvatar";
import AES from "@/assets/js/AES.js";
import { checkPermiss } from "../assets/js/utlis";

export default {
  name: "Navigation",
  components: { UploadAvatar },
  props: ["printList", "workLabel","list"],
  data() {
    return {
      userName: null,
      dialogVisible: false,
      dialogTVisible: false,
      imgDis: false,
      formLabelAlign: {
        oldPassword: "",
        newPassword: ""
      },
      formLabelAlignT: {
        oldPassword: "",
        newPassword: ""
      },
      noauthorize: false,
      authorize: true,
      rules: {
        oldPassword: [
          { required: true, message: "请输入密码", trigger: "change" },
          {
            min: 6,
            max: 12,
            message: "长度在 6 到 12 个字符",
            trigger: "change"
          }
        ],
        newPassword: [
          { required: true, message: "请输入密码", trigger: "change" },
          {
            min: 6,
            max: 12,
            message: "长度在 6 到 12 个字符",
            trigger: "change"
          }
        ]
      },
      dataList: [],
      data: [],
      listArr: [],
      // list: [],
      httpImg: "",
      eliminate: false,
      golbalPermissions: []
    };
  },
  created() {
    this.userName = localStorage.getItem("approvedname");
     this.url = sessionStorage.getItem("url");

    // this.menuListQuthority(url)
    // this.menuList(url);
    this.getHttpImg();
    this.data = this.printList;
    this.golbalPermissions = localStorage.getItem("permissions").split(",");
  },
  watch:{
    list(val, value){
      this.menuListQuthority(  this.url)
    },
  },
  computed: {
    //订单更新按钮
    showBig: function() {
      return checkPermiss("big:data:monitor", this.golbalPermissions);
    }
  },
  updated() {
    this.scrollMenu();
  },
  methods: {

    //处理菜单权限
    menuListQuthority(url){
      let listArr = [];
      this.getList(this.list, listArr);
      this.listArr = listArr;
      if (url == "/Index/workbench" || url == null) {
        return;
      } else {
        let printName = this.listArr.find(item => {
          return item.url == url;
        });
        this.data.push(printName);
        $(".work ").removeClass("workActive");
        $(".scrollMenu ").addClass("active");
      }
    },
    // 头像
    getHttpImg() {
      getIcon().then(res => {
        if (res.data != null) {
          this.httpImg = res.data;
        } else if (res.data == null) {
          this.httpImg = imgSrc;
        }
      });
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then(() => {
          done();
          this.eliminate = true;
          this.$refs.UploadAvatar.formResetFields(true);
        })
        .catch(() => {});
    },
    selectOpen(obj) {
      if (obj.length >= 0) {
        this.data = obj;
        $(".work ").removeClass("workActive");
      }
    },
    imgFun() {
      this.imgDis = true;
    },
    getData(val) {
      if (val) {
        this.httpImg = val;
        this.imgDis = false;
      }
    },

    //将树形数据转为一维数组
    getList(treeData, list) {
      treeData.map(item => {
        let { id, url, label, otherId, pid } = item;
        list.push({ id, url, label, otherId, pid });
        if (item.children != null) {
          this.getList(item.children, list);
        }
      });
    },
    // 菜单
    menuList(url) {

      menuList().then(res => {
        if (res.code == 200) {
          res.data.splice(res.data.indexOf(res.data[0]), 1);
          this.list = res.data;
          let listArr = [];
          this.getList(this.list, listArr);
          this.listArr = listArr;
          if (url == "/Index/workbench" || url == null) {
            return;
          } else {
            let printName = this.listArr.find(item => {
              return item.url == url;
            });
            this.data.push(printName);
            $(".work ").removeClass("workActive");
            $(".scrollMenu ").addClass("active");
          }
        }
      });
    },
    //修改密码
    updateFun() {
      this.$refs.formLabelAlign.validate().then(() => {
        let keys = "drxfdsjcomwssdbc";
        let enNewPassword = AES.encrypt(this.formLabelAlign.newPassword, keys);
        let enOldPassword = AES.encrypt(this.formLabelAlign.oldPassword, keys);
        let encrypts = {};
        encrypts.newPassword = enNewPassword;
        encrypts.oldPassword = enOldPassword;
        apiUpdatePassword(encrypts).then(res => {
          if (res.code == 200) {
            this.$message({
              message: res.message,
              type: "success"
            });
            this.$router.push({ path: "/" });
            sessionStorage.clear();
            this.$refs["formLabelAlign"].resetFields();
          } else {
            this.$message({
              message: res.message,
              type: "error"
            });
          }
        });
      });
    },
    // 点击工作台跳转
    workbench() {
      this.$emit("showClass", true);
      $(".scrollMenu ").removeClass("active");
      $(".work ").addClass("workActive");
      this.$router.push({
        path: "/Index/workbench"
      });
      sessionStorage.setItem("url", "/Index/workbench");
    },
    // 点击大数据跳转
    webBig() {
      
      // window.location.href = 'http://192.168.0.204:8080';
      if (process.env.NODE_ENV == "production") {
        window.location.href = "https://yun.xssbdc.com/monitor/";
        // getBigWbe().then(res => {
        //   if(res.code && res.code !== 200) {
        //     this.$message.error("无权限访问!")
        //   }
        // })
      } else {
       
       
        window.location.href = `http://192.168.0.204:8080/?token=${localStorage.getItem(
          "token"
        )}`;
      }
    },
    //修改授权密码
    updateAuthFun() {
      this.$refs.formLabelAlignT.validate().then(() => {
        let keys = "drxfdsjcomwssdbc";
        let enNewPassword = AES.encrypt(this.formLabelAlignT.newPassword, keys);
        let enOldPassword = AES.encrypt(this.formLabelAlignT.oldPassword, keys);
        let encrypts = {};
        encrypts.newPassword = enNewPassword;
        encrypts.oldPassword = enOldPassword;
        apiUpdatePasswordT(encrypts).then(res => {
          if (res.code == 200) {
            this.$message({
              message: res.message,
              type: "success"
            });
            this.$refs["formLabelAlignT"].resetFields();
          } else {
            this.$message({
              message: res.message,
              type: "error"
            });
          }
        });
      });
    },
    //取消按钮
    cancle(obj) {
      if (obj) {
        this.dialogTVisible = false;
        this.$refs.formLabelAlignT.resetFields();
      } else {
        this.dialogVisible = false;
        this.$refs.formLabelAlign.resetFields();
      }
    },
    signOut() {
      apiLogout().then(() => {
        this.$router.push({ path: "/" });
        localStorage.clear();
        sessionStorage.clear();
      });
    },
    updateTag(item, index) {
      //获取路由路径
      let path = this.$route.path;
      let indexMenu = this.data.findIndex(i => i.label === item.label);
      let indexCurr = this.printList.findIndex(i => i.url === path);
      let urlIndex = indexMenu - 1;
      let urlIndexF = indexMenu;
      if (path == item.url && this.printList.length != 1) {
        //当删除的是第一个
        if (indexMenu == 0) {
          let data = this.printList.find(item => {
            return item.url == this.data[urlIndexF + 1].url;
          });
          if (data.otherId != null && data.otherId != undefined) {
            sessionStorage.setItem("prductId", data.otherId);
          }
          this.$router.push(this.data[urlIndexF + 1].url);
          $(".scrollMenu ")
            .eq(urlIndexF)
            .addClass("active")
            .siblings()
            .removeClass("active");
        } else {
          //删除的是后面的
          let data = this.printList.find(item => {
            return item.url == this.data[urlIndex].url;
          });
          if (data.otherId != null && data.otherId != undefined) {
            sessionStorage.setItem("prductId", data.otherId);
          }
          this.$router.push(this.data[urlIndex].url);
          sessionStorage.setItem("url", this.data[urlIndex].url);
          $(".scrollMenu ")
            .eq(urlIndex)
            .addClass("active")
            .siblings()
            .removeClass("active");
        }
      } else if (indexCurr > index) {
        $(".scrollMenu ")
          .eq(indexCurr - 1)
          .addClass("active")
          .siblings()
          .removeClass("active");
      }
      this.data = this.data.filter(i => {
        return i != item;
      });
      this.$emit("print", this.data);
      if (this.data.length <= 0) {
        this.$router.push("/Index/workbench");
        sessionStorage.setItem("url", "/Index/workbench");
        $(".work ").addClass("workActive");
      }
    },
    clickTag(item) {
      let path = this.$route.path;
      if (item.url != path) {
        if (item.otherId != null && item.otherId != undefined) {
          sessionStorage.setItem("prductId", item.otherId);
        }
        this.$router.push(item.url);
        sessionStorage.setItem("url", item.url);
      }
      $(".work ").removeClass("workActive");
    },
    //点击图标
    scrollMenu() {
      $(".scrollMenu ").on("click", function() {
        $(this)
          .addClass("active")
          .siblings()
          .removeClass("active");
        let left = $(this).position().left;
        let objWidth = $(this).width();
        let width = $(".tag-group").width();
        let objLeft = objWidth + width;
        if (left > width) {
          $(".tag-group").animate({ scrollLeft: objLeft + "px" }, 500);
        } else if (left < 0) {
          $(".tag-group").animate({ scrollLeft: left + 20 + "px" }, 500);
        }
      });
    }
  }
};
</script>
<style scoped type="less">
.box {
  padding: 0;
  margin: 0;
  width: calc(100% - 210px);
  height: 58px;
  position: fixed;
  right: 0;
  z-index: 99;
  background-color: white;
  overflow: hidden;
  box-shadow: 3px 1px 5px rgb(193, 193, 193);
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.header-name {
  width: 200px;
  float: left;
  height: 100%;
}
.name {
  float: right;
  line-height: 60px;
  font-size: 14px;
  height: 100%;
  color: #333333;
}
.header-crumb {
  float: left;
  height: 100%;
  margin-left: 20px;
}
.el-breadcrumb {
  line-height: 60px;
}

.header-right {
  float: right;
  font-size: 14px;
  color: #666666;
  height: 100%;
}
.headImg {
  width: 50px;
  height: 50px;
  float: left;
  margin-top: 3px;
  margin-right: 5px;
  border-radius: 25px;
}
/* .el-avatar {
  float: left;
  margin-top: 9px;
} */
.p1 {
  float: left;
  height: 100%;
  line-height: 58px;
  margin-right: 20px;
}
.header-right p:hover {
  cursor: pointer;
}
.tag-group {
  text-align: left;
  float: left;
  height: 58px;
  line-height: 58px;
  width: calc(100% - 450px);
  margin-left: 20px;
  overflow-x: auto; /* 添加横向滚动条 */
  white-space: nowrap; /*不换行*/
  overflow-y: hidden;
  /* overflow: hidden; */
}
::-webkit-scrollbar {
  height: 8px;
  background-color: rgb(241, 243, 244);
  border-radius: 25px;
}
.tag-group .el-tag {
  margin-left: 10px;
  cursor: pointer;
}
/* .el-tag.el-tag--success {
  height: 32px;
  border: 1px solid rgba(238, 238, 238, 1);
  border-radius: 4px;
  color: #333333;
  background: rgba(255, 255, 255, 1);
}
::v-deep .el-tag.el-tag--success .el-tag__close {
  color: #333333;
}
::v-deep .el-tag.el-tag--success:hover .el-tag__close {
  color: rgba(59, 157, 248, 1);
  background: none;
}
::v-deep .el-tag.el-tag--success:hover {
  cursor: pointer;
  border: 1px solid rgba(59, 157, 248, 1);
  border-radius: 4px;
  color: rgba(59, 157, 248, 1);
  background-color: rgba(59, 157, 248, 0.1);
} */
.scrollMenu {
  color: #333333;
  border: 1px #eeeeee solid;
}
::v-deep .el-tag--plain .el-tag__close {
  color: #333333;
}
::v-deep .el-tag--plain .el-tag__close:hover {
  background: none;
}
.scrollMenu.active {
  background-color: rgba(59, 157, 248, 0.2);
  color: #3b9df8;
  border-color: #3b9df8;
}
::v-deep .active .el-icon-close {
  color: #3b9df8;
}
::v-deep .workNav {
  background-color: rgba(59, 157, 248, 0.2);
  color: #3b9df8;
  border-color: #3b9df8;
}
.userName {
  padding-right: 10px;
  border-right: 2px #eeeeee solid;
  height: 20px;
  line-height: 20px;
  margin-top: 20px;
  margin-right: 10px;
  float: left;
  max-width: 60px;
  overflow: hidden;
  display: block;
}
::v-deep .el-icon-edit-outline:before {
  margin-right: 2px;
}
::v-deep .el-icon-alarm-clock:before {
  margin-right: 2px;
}
.work {
  width: 100px;
  height: 32px;
  border: 1px solid #eeeeee;
  border-radius: 4px;
  background-color: transparent;
  color: #333333;
  text-align: center;
}
.workActive {
  background-color: rgba(59, 157, 248, 0.2);
  color: #3b9df8;
  border-color: #3b9df8;
}
</style>
